package cn.ulc.socket.common.helper;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.util.ConvertUtil;
import cn.ulc.common.util.GlobalConfig;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;

import java.math.BigDecimal;

public class MiniAppPayHelper {

    private static final Log log = LogFactory.get();
    /**
     * 根据订单 和 用户Ip 来生成 WxPayUnifiedOrderRequest
     * @param orderNo
     * @param amount
     * @param userIp
     * @return
     */
    public static WxPayUnifiedOrderRequest prepayInfo(String orderNo, BigDecimal amount, String userIp , String openId) {
        log.info("==============Order : " + orderNo + " === "+amount);
//        Double payAmount = order.amount * 100;
        WxPayUnifiedOrderRequest prepayInfo = WxPayUnifiedOrderRequest.newBuilder()
                .notifyUrl( GlobalConfig.WEIXIN_PAY_BACK_URL)
                .openid(openId)
                .outTradeNo(orderNo)
                .totalFee(BaseWxPayRequest.yuanToFen(ConvertUtil.toString(amount)))
                .body("订单支付:" + orderNo) // todo 修改
                .tradeType("JSAPI")
                .spbillCreateIp(userIp)
                .build();
        log.info("Log 100011 prepayInfo : " +prepayInfo);
        return prepayInfo;
    }

    public static WxPayRefundRequest refundInfo(String orderNo,String refundNo,String transactionId, BigDecimal orderAmount,BigDecimal refundAmount) {
        WxPayRefundRequest refundInfo = WxPayRefundRequest.newBuilder().build();
        refundInfo.setTransactionId(transactionId);
        refundInfo.setOutRefundNo(refundNo);
        refundInfo.setOutTradeNo(orderNo);
        refundInfo.setTotalFee(BaseWxPayRequest.yuanToFen(ConvertUtil.toString(orderAmount)));
        refundInfo.setRefundFee(BaseWxPayRequest.yuanToFen(ConvertUtil.toString(refundAmount)));
        return refundInfo;
    }

    /**
     * 企业支付 返回 EntPayRequest
     * @param
     * @return
     * 目前不用 ，用的时候放开注释修改
     */
//    public static EntPayRequest entpayInfo(BillFlow billFlow) {
//        WebUser webUser = WebUser.findById(billFlow.webUser.id);
//        EntPayRequest entPayRequest = new EntPayRequest();
//        entPayRequest.setAppid(GlobalConfig.MINIAPP_APP_ID);
//        entPayRequest.setMchId(GlobalConfig.MINIAPP_MCH_ID);
//        entPayRequest.setNonceStr(ConvertUtil.toMD5(RandomNumberUtil.generateRandomChars(6)));
//        entPayRequest.setPartnerTradeNo(billFlow.flowNumber);
//        entPayRequest.setOpenid(webUser.openId);
//        entPayRequest.setCheckName("NO_CHECK");
//        entPayRequest.setAmount(BaseWxPayRequest.yuanToFen(ConvertUtil.toString(billFlow.flowAmount)));
//        entPayRequest.setDescription(webUser.nickName + "于" + DateUtil.formatDateTime(new Date()) + "提现" +  billFlow.flowAmount +"元");
//        entPayRequest.setSpbillCreateIp(GlobalConfig.SPBILL_CREATE_IP);
//        return entPayRequest;
//    }

}
